﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufBase.h"
#include "GnufComponent.h"
#include "GnufItemEvents.h"
#include "GnufPallet.h"

/// \file GnufDynamicPanelBase.h
/// \brief 动态面板的基类

namespace gnuf {

    class Block;
    class ButtonEx;
    class DropDownBoard;
    class Info;
    class Pallet;
    class CustomizedPreferenceEntryCreatorRegisterer;

    /// \brief Block 构建器
    /// \see Block IToolbarWatcher ButtonEx
    class GNUF_BASE_EXPORT BlockBuilderBase
    {
    public:
        BlockBuilderBase(Block * block, IToolbarWatcher* toolbarWatcher, EButtonDisplayStyle style);
        virtual ~BlockBuilderBase();
        /// \brief 在指定的Block中添加按钮
        /// \param id 添加的按钮的ID
        /// \param wantToClick Block创建完毕后是否点击该按钮，默认为false
        /// \param wantToSelect Block创建完毕后是否选中该按钮，默认为false (这个参数暂时不起作用 --20220329）
        /// \param info 按钮的Info
        /// \see Info 
        virtual void addButton(const std::wstring & id,
            bool wantToClick = false,
            bool wantToSelect = false,
            gcmp::OwnerPtr< Info > info = nullptr);
    protected:
        /// \brief 对应的Block
        Block * m_block;
        /// \brief 创建完毕后点击的按钮
        ButtonEx * m_button;
        /// \brief 绑定的ToolBar监视器
        IToolbarWatcher * m_toolbarWatcher;
        EButtonDisplayStyle m_buttonStyle;
    };

    /// \brief 动态面板的基类
    /// \see Component IItemEventHandler
    class GNUF_BASE_EXPORT DynamicPanelBase 
        : public Component
        , public IItemEventHandler
        , public IPalletEventHandler
    {
        DEFINE_CAST_BASE(DynamicPanelBase)
    public:
        DynamicPanelBase(const std::wstring & key, const gcmp::UniqueString * uniqueKey, const std::wstring& configuration, bool wantSwitchButtonDisplayStyle);
        virtual ~DynamicPanelBase() ;
        EButtonDisplayStyle buttonDisplayStyle() const { return m_buttonStyle; }

        /// \brief 设置动态面板的按钮样式
        /// \param style 样式，支持仅图标，图标加文字
        /// \param firstTime 是否初次设置按钮样式，决定设置样式之前是否做Diff
        /// \retval true 动态面板的按钮样式刷新了
        /// \retval false 动态面板的按钮样式没有刷新
        bool setButtonDisplayStyle(EButtonDisplayStyle style, bool firstTime = false);

    protected:
        // Inherits from IItemEventHandler
        virtual void onQtMousePressed(Toolbar* toolbar, void* evt) override;
        virtual void onCheckStatusChanged(CheckButton* checkButton) override;
        virtual void onHovered(Item*, bool hovered, void* qtWidget) override;

        // Inherits from IPalletEventHandler...
        virtual void onHeaderButtonClicked(Pallet*) override;

        void createContextMenu();
        gcmp::OwnerPtr< Pallet > m_pallet;
        gcmp::OwnerPtr<DropDownBoard> m_contextMenu;
    private:
        bool m_wantSwitchButtonStyle = true;
        EButtonDisplayStyle m_buttonStyle = EButtonDisplayStyle::eDefault;
    };
}
